import sys
from BasicLibraries import *
import _A_create_landscapes as cl
import _B_get_feature_importance as gfi
import warnings
warnings.filterwarnings("ignore", category=RuntimeWarning) 
warnings.filterwarnings("ignore", category=FutureWarning) 

ONEDRIVE = ''

#==== Binarisation quantile 
BQL=str(0.75)
#=== Default choice to control for energy prices
energy_control = False
print('Binarisation threshold:', BQL)
print('Control for energy prices for robustness tests:', energy_control)
BQL = float(BQL)


optimization_method = 'GA'
#==== Choose your population
population_type_ = 'LandscapePopulation_0724_full.txt'
feature_type_ = 'opt_'+optimization_method+'_feature_importance_0724.pckl'
process_type_ = 'opt_'+optimization_method+'_process_data_0724.pckl'
stat_type_ = 'opt_'+optimization_method+'_deviation_stat_0724.pckl'
tmp_outputs_ = 'tmp_outputs/opt_'+optimization_method+'_forest_'
#====

#=== Optimisation characteristics
parent_selection_option = 'sss'
crossover_option =  ['single_point', 'uniform']
mutation_types_option = ['random', 'swap']
#=== Type of permutation test for feature selection
permutation_test_type = ['impurity', 'permutation'][1]

#================
np.random.seed(5)
#================

ACTIONS = [  'association',
             'adoption of standards and rules',
             'assessment and measurement',
             'organizational structuring',
             'modification of procedures',
             'asset modification',
             'training',
             'r&d investments',
             'new products',
     ]
sdgs = ['water & energy', 'cons. & prod.', 'biodiversity']

full_initiatives = list(np.ravel([[A+' - SDG '+S for A in ACTIONS] for S in sdgs]))

#===============
up_to = 2021
#===============

#%%
if __name__ == '__main__':
    for FW in [0.,0.25,0.5, 0.75,1.]:
        #=== name the output files
        population_type = str(FW).replace('.', '_')+'_'+population_type_
        feature_type = str(FW).replace('.', '_')+'_'+feature_type_
        process_type = str(FW).replace('.', '_')+'_'+process_type_
        stat_type = str(FW).replace('.', '_')+'_'+stat_type_
        tmp_outputs = tmp_outputs_+str(FW).replace('.', '_')+'_'
        #=== Read the data
        dt = pd.read_csv(population_type)

        print('Latest tested year:', up_to, 'FW:', FW)
        dt = dt[(dt.rfyear > 2011)]

        #== Create the landscape and save the output in tmp_outputs
        normal_fit = False # If True,  then you do internal cross-validation. Otherwise, you run an explict cross validation step
        cl.create_landscape_files(FW, dt, ACTIONS, sdgs,  tmp_outputs, parent_selection_option,
                               crossover_option, mutation_types_option, normal_fit=normal_fit,
                               allocation_type=optimization_method,
                               final_year = up_to,energy_control=energy_control)

        #== Feature importance
        gfi.get_feature_importance_(dt, ACTIONS, sdgs, normal_fit, population_type, feature_type,
                                    permutation_test_type,
                                    final_year = up_to,
                                    save_output=True,energy_control=energy_control)
    
    
    
    
    
    
    
